<?php

/**
 * Index.php BIXI
 *
 * @author tmontel
 */

require_once 'BixiManager.php';
require_once 'LocationManager.php';
require_once("system/HttpRequest.php");

$bixiStations;
$interestPoints;

// ajax.php

if(Request('bixi') && Request('latMin') && Request('lngMin') && Request('latMax') && Request('lngMax')) {
    $bixiManager = new BixiManager();
    $bixiStations = $bixiManager->getBixiLocations(Request('latMin'), Request('lngMin'), Request('latMax'), Request('lngMax'));
    echo $bixiStations;
    die();
}

if(Request('interest') && Request('latMin') && Request('lngMin') && Request('latMax') && Request('lngMax')) {
    $locationManager = new LocationManager();
    $interestPoints = $locationManager->getLocations(Request('latMin'), Request('lngMin'), Request('latMax'), Request('lngMax'));
    echo $interestPoints;
    die();
}

?>
<html>
    <head>
        <meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
        <script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false"></script>
        <script src="js/ext-core.js"></script>
        <script src="js/JSON.js"></script>
        <script type="text/javascript">


            var geocoder;

            var map;

            var markersArray = [];


            function initialize() {

                // Start settings MAP

                var latlng = new google.maps.LatLng(45.5454470,-73.6390760);
                //var latlng = new google.maps.LatLng(0,0);

                var myOptions = {
                    zoom: 16,
                    center: latlng,
                    scaleControl: true,
                    mapTypeId: google.maps.MapTypeId.ROADMAP

                };


                map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);

                geocoder = new google.maps.Geocoder();


                // End settings MAP

                google.maps.event.addListener(map, 'dragend', function() {
                    placeMarkers();
                });

                google.maps.event.addListener(map, 'dragstart', function() {
                    deleteOverlays();
                });

            }

            function placeMarkers(){
                var cornerSW = map.getBounds().getSouthWest();
                var cornerNE = map.getBounds().getNorthEast();
                getBixiMarkers(cornerSW.lat(), cornerSW.lng(), cornerNE.lat(), cornerNE.lng());
                getLocationMarkers(cornerSW.lat(), cornerSW.lng(), cornerNE.lat(), cornerNE.lng());
            }

            function getLocationMarkers(latMin, lngMin, latMax, lngMax){
                Ext.Ajax.request({
                    url:'index.php',
                    method:'GET',
                    success: function(response){
                        var mark = JSON.decode(response.responseText);
                        for(i = 0; i < mark.length; i++){
                            setStation(mark[i].lat, mark[i].lng, mark[i].title, mark[i].desc, 'pi');
                        }
                        showOverlays();
                    },
                    failure: function() {
                        alert("Something fail");
                    },
                    params: { interest: 'interest', latMin: latMin , lngMin: lngMin , latMax: latMax , lngMax: lngMax}
                });
            }

            function getBixiMarkers(latMin, lngMin, latMax, lngMax){
                Ext.Ajax.request({
                    url:'index.php',
                    method:'GET',
                    success: function(response){
                        var mark = JSON.decode(response.responseText);
                        for(i = 0; i < mark.length; i++){
                            setStation(mark[i].lat, mark[i].lng, mark[i].title, mark[i].desc, 'bixi');
                        }
                        showOverlays();
                    },
                    failure: function() {
                        alert("Something fail");
                    },
                    params: { bixi: 'bixi', latMin: latMin , lngMin: lngMin , latMax: latMax , lngMax: lngMax}
                });
            }

            function setStation(x, y, stitle, infobulle, type) {
                var myLatlng = new google.maps.LatLng(x, y);

                var marker = new google.maps.Marker({
                    position: myLatlng,
                    title: stitle
                });

                marker.setMap(map);
                if (type == 'bixi')
                {
                    marker.setIcon('./img/bixi.png');
                }
                else
                {
                    marker.setIcon('./img/pi.png');
                }

                var contentString = '<div id="content">' + infobulle + '</div>';

                var infowindow = new google.maps.InfoWindow({
                    content: contentString
                });

                google.maps.event.addListener(marker, 'click', function() {
                    infowindow.open(map,marker);
                });

                markersArray.push(marker);

            }

            function showOverlays() {
                if (markersArray) {
                    for (i in markersArray) {
                        markersArray[i].setMap(map);
                    }
                }
            }

            function deleteOverlays() {
                if (markersArray) {
                    for (i in markersArray) {
                        markersArray[i].setMap(null);
                    }
                    //markersArray.length = 0;
                }
            }

            function codeAddress() {
                var address = document.getElementById("address").value;
                if (geocoder) {
                    geocoder.geocode( { 'address': address}, function(results, status) {
                        if (status == google.maps.GeocoderStatus.OK) {
                            map.setCenter(results[0].geometry.location);
                        } else {
                            alert("Geocode was not successful for the following reason: " + status);
                        }
                    });
                }

            }
            function moveToDarwin() {
                var darwin = new google.maps.LatLng(-12.461334, 130.841904);
                map.setCenter(darwin);
            }

            function placeMarker(location) {
                var clickedLocation = new google.maps.LatLng(location);
                var marker = new google.maps.Marker({
                    position: location,
                    map: map
                });
                map.setCenter(location);
            }

        </script>
    </head>
    <body onload="initialize()">

        <div>
            <input id="address" type="textbox" value="Montreal, QC">
            <input type="button" value="Search" onclick="codeAddress()">
        </div>

        <div id="map_canvas" style="width:800px; height:600px"></div>
        <div id="map_infos" style="width:200px; height:600px"></div>
    </body>
</html>